package com.decoration.basic.service.impl;

import java.util.*;

import javax.annotation.Resource;

import com.alibaba.druid.util.StringUtils;
import com.decoration.basic.entity.*;
import com.decoration.basic.mapper.*;
import com.decoration.basic.po.DecCtctionInfVo;
import com.decoration.basic.findCondition.DecFlowFindCondition;
import com.decoration.basic.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.decoration.basic.entity.DecUserBase;
import com.decoration.basic.entity.DecorationCtctionRelation;
import com.decoration.basic.entity.DecorationPicture;
import com.decoration.basic.entity.DecorationUser;
import com.decoration.basic.mapper.DecCtcRelMapper;
import com.decoration.basic.findCondition.DecCtctionFindCondition;
import com.decoration.basic.service.DecCtctionService;

@Service
public class DecCtctionServiceImpl implements DecCtctionService {

    // 日志记录器
    private static final Logger log = LoggerFactory.getLogger(DecCtctionServiceImpl.class);

    @Resource
    private DecCtctionMapper decCtctionMapper;

    @Resource
    private DecCtcRelMapper decCtcRelMapper;

    @Resource
    private DecUserMapper decUserMapper;

    @Resource
    private DecPictureMapper decPictureMapper;

    @Resource
    private DecCtctionPhaseMapper decCtctionPhaseMapper;

    @Resource(name = "ComServiceImpl")
    private ComServiceImpl comService;

    /**
     *  删除项目详情信息，项目关联用户信息，项目材料图片信息
     * @param ctcid
     * @return
     * @throws DataAccessException
     */
    @Override
    public Boolean delete(String ctcid) throws DataAccessException {

        //删除项目详情信息
        int num = decCtctionMapper.delete(ctcid);
        Boolean result = (num == 1 ? Boolean.TRUE : Boolean.FALSE);

        //删除项目关联用户信息
        int num1 = decCtcRelMapper.deleteCtcRel(ctcid);
        log.info(num1 > 0 ? "无项目关联信息删除":"项目关联信息删除条数:["+String.valueOf(num1));

        //删除项目关联材料/项目计划图片
        int num2 = decPictureMapper.deleteCtcRelPic(ctcid);
        log.info(num2 > 0 ? "无项目材料图片删除":"项目材料图片删除条数:["+String.valueOf(num2));
        return result;
    }

    /**
     *   更新项目信息，包括材料图片及项目关联用户
     * @param inMsg
     * @param userid
     * @return
     * @throws DataAccessException
     */
    @Override
    public Boolean update(DecCtctionInMsg inMsg, String userid) throws DataAccessException {

        //1.项目信息更新
        DecCtctionPo inf = null;
        DecCtctionPo updateInf = new DecCtctionPo();
        try {
            inf = decCtctionMapper.findByctcid(inMsg.getCtcid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (inf == null){
            System.out.println("项目不存在:"+inMsg.getCtcid());
            return Boolean.FALSE;
        }
        if ( inf.getCtcProgress() == 2){
            System.out.println("已完工项目不允许修改");
            return Boolean.FALSE;
        }
        //项目内容修改
        updateInf.setCtcid(inf.getCtcid());
        updateInf.setCtcAddress(inMsg.getCtcAddress());
        updateInf.setCtcName(inMsg.getCtcName());
        updateInf.setCtcDesc(inMsg.getCtcDesc());
        updateInf.setCtcType(inMsg.getCtcType());
        updateInf.setStartTime(inMsg.getStartTime());
        updateInf.setForecastTime(inMsg.getForecastTime());
        updateInf.setRemark(inMsg.getRemark());
        if (inMsg.getCtcProgress() != null){
            updateInf.setCtcProgress(inMsg.getCtcProgress());
        }

        if (inMsg.getCtcProgress() != null && inMsg.getCtcProgress() ==2){
            updateInf.setEndTime(DateFormatUtils.getNowDay("yyyy-MM-dd"));
        }

        if (inMsg.getCtcPlan() !=null && inMsg.getCtcPlan().size() >0){
            updateInf.setCtcPlan(StringUtil.arrayAsStr(inMsg.getCtcPlan()));
        }

        /*if (inMsg.getMatlist().size() >0){
            //删除原材料图片、新增本材料图片
            decPictureMapper.deleteMatlist(inMsg.getCtcid());
            for (String address : inMsg.getMatlist()){
                DecorationPicture inpic = new DecorationPicture();
                inpic.setPicAddress(address);
                inpic = DecCtctionUtil.buildDecPicture(inpic,inf);
                decPictureMapper.add(inpic);
            }
        }*/

        String picId = comService.getIdByAddress(inMsg.getPicAdd());
        updateInf.setPicId(picId);

        updateInf.setUpdateTime(new Date());
        updateInf.setUpdateId(userid);
        //以下空指针问题
       if (StringUtil.isEmpty(inMsg.getFlowId()) || StringUtil.isEmpty(inf.getFlowId()) ||
               (StringUtil.isNotEmpty(inMsg.getFlowId()) && StringUtil.isNotEmpty(inf.getFlowId())
                       && ! inf.getFlowId().equals(inMsg.getFlowId()))){
            if (StringUtil.isEmpty(inMsg.getFlowId())){
                //需要去校验是否已填入师傅，时间等信息，并且项目处于第一个阶段未申请状态

            }

            //修改流程状态
            int flowstats = 1;
            try {
                flowstats = decCtctionPhaseMapper.checkFlowStats(inf.getCtcid());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (flowstats == 0 ){ //修改关联流程
                updateInf.setFlowId(inMsg.getFlowId());
                try {
                    decCtctionPhaseMapper.deleteFlowByCtcId(inf.getCtcid());
                    decCtctionPhaseMapper.deletePhaseByCtcId(inf.getCtcid());
                    if ( StringUtil.isNotEmpty(inMsg.getFlowId())){
                        buildRealFlow(userid,inMsg.getFlowId(),inf.getCtcid());
                    }

                } catch (Exception e) {
                    System.out.println("重置关联流程异常、先删后新增");
                    e.printStackTrace();
                }
            }
        }

        //修改项目内容
        int num = decCtctionMapper.update(updateInf);
        if (num != 1) {
            return Boolean.FALSE;
        }

        // 2.项目相关用户信息修改: 先删除，后添加
        int rel = decCtcRelMapper.deleteCtcRel(inMsg.getCtcid());

        log.info("删除数据量为:["+String.valueOf(rel));
        List<DecUserBase> newDesignPapers = this.makeUser(inMsg.getDesignPapers(), 4);
        List<DecUserBase> newSuplist = this.makeUser(inMsg.getSuplist(), 2);
        List<DecUserBase> newUserslist = this.makeUser(inMsg.getUserslist(), 3);
        List<DecUserBase> newlist = this.mergeList(newDesignPapers,  newSuplist,  newUserslist);

        this.addCtcRelUsers(newlist, inMsg.getCtcid(), userid);

        // 3.项目材料图片信息修改
        this.addPicture(inMsg.getMatlist(), inMsg.getCtcid(), userid);

        return Boolean.TRUE;
    }

    /**
     *   新增项目信息，包括项目关联及材料图片
     * @param inMsg
     * @param user
     * @return
     * @throws DataAccessException
     */
    @Override
    public Boolean add(DecCtctionInMsg inMsg, DecorationUser user) throws DataAccessException {
        //1.新增项目信息
        DecCtctionPo inf = new DecCtctionPo();

        String ctcid = SnowFlake.getInstance().getNewUuid();
        inf.setCtcid(ctcid);

        inf.setCtcAddress(inMsg.getCtcAddress());
        inf.setCtcName(inMsg.getCtcName());
        //项目类型默认为null
        /*if (inMsg.getCtcType() == null ){
            inf.setCtcType(0);
        }else {
            inf.setCtcType(inMsg.getCtcType());
        }*/
        try {
            inf.setCtcType(inMsg.getCtcType());
        } catch (Exception e) {
            e.printStackTrace();
            inf.setCtcType(null);
        }
        if (inMsg.getCtcPlan().size() >0){
            inf.setCtcPlan(StringUtil.arrayAsStr(inMsg.getCtcPlan()));
        }
        inf.setCtcDesc(inMsg.getCtcDesc());

        try {
            inf.setStartTime(inMsg.getStartTime()==null? DateFormatUtils.getNowDay("yyyy-MM-dd"): inMsg.getStartTime());
        } catch (Exception e) {
            e.printStackTrace();
            inf.setStartTime(null);
        }

        String picId = comService.getIdByAddress(inMsg.getPicAdd());
        inf.setPicId(picId);

        inf.setForecastTime(inMsg.getForecastTime());
        inf.setFlowId(inMsg.getFlowId());
        inf.setCtcProgress(0);
        inf.setRemark(inMsg.getRemark());

        inf.setUpdateTime(new Date());
        inf.setUpdateId(user.getUserId());
        inf.setCreateTime(new Date());
        inf.setCreateId(user.getUserId());

        //新增项目信息
        int num = decCtctionMapper.add(inf);
        if (num != 1) {
            return false;
        }

        //2.新增项目用户信息
        //设计师
        List<DecUserBase> newDesignPapers = this.makeUser(inMsg.getDesignPapers(), 4);
        //业主
        List<DecUserBase> newUserslist = this.makeUser(inMsg.getUserslist(), 3);
        //监理
        List<DecUserBase> newSuplist = this.makeUser(inMsg.getSuplist(), 2);

        List<DecUserBase> newlist = this.mergeList(newDesignPapers,newSuplist, newUserslist);

        // 新增项目用户信息
        this.addCtcRelUsers(newlist, ctcid, user.getUserId());

        //3.新增项目关联流程信息
        if (! StringUtils.isEmpty(inMsg.getFlowId())){

            try {
                buildRealFlow(user.getUserId(),inMsg.getFlowId(),ctcid);
            } catch (Exception e) {
                System.out.println("新增关联流程异常");
                e.printStackTrace();
            }
        }
        // 4.新增相关材料，项目计划图片信息
        if (inMsg.getMatlist() != null) {
            this.addPicture(inMsg.getMatlist(), ctcid, user.getUserId());
        }

        return true;
    }

    /**
     * 管理系统平台 查找  项目详情
     * @param ctcid
     * @return
     */
    @Override
    public DecCtctionOutMsg searchCtcInfo(String ctcid) {
        DecCtctionOutMsg outMsg = new DecCtctionOutMsg();
        //1.查找项目详情
        DecCtctionPo result = decCtctionMapper.findByctcid(ctcid);
        if (result == null) {
            return null;
        }

        buildDecCtctionOutMsg(outMsg,result);

        if ( ! StringUtils.isEmpty(result.getCtcPlan())){
            outMsg.setCtcPlan(StringUtil.StrAsArray(result.getCtcPlan()));
        }

        //2.查找项目用户列表
        List<DecUserBase> alluser = decCtcRelMapper.findCtcRelById(ctcid);
        //    相关监理用户列表
        List<String> suplist = new ArrayList<>();
        //    相关业主用户列表
        List<String> userslist = new ArrayList<>();
        //    设计师列表String
        List<String> designPapers = new ArrayList<>();

        //相关名称
        String usersStr = "";
        String designStr = "";
        String supStr = "";

        for (DecUserBase base : alluser) {
            switch (base.getPerType()) {
                case 2:
                    suplist.add(base.getUserId());
                    if (supStr.equals("")){
                        supStr = supStr + base.getPerName();
                    }else {
                        supStr = supStr +","+ base.getPerName();
                    }

                    break;
                case 3:
                    userslist.add(base.getUserId());
                    if (usersStr.equals("")){
                        usersStr = usersStr + base.getPerName();
                    }else {
                        usersStr = usersStr +","+ base.getPerName();
                    }

                    break;
                case 4:
                    designPapers.add(base.getUserId());
                    if (designStr.equals("")){
                        designStr = designStr + base.getPerName();
                    }else {
                        designStr = designStr +","+ base.getPerName();
                    }

                    break;
                default:
                    log.info("项目关联信息异常" + base.getPerName());
                    break;
            }
        }
        outMsg.setSuplist(suplist);
        outMsg.setUserslist(userslist);
        outMsg.setDesignPapers(designPapers);
        outMsg.setSupStr(supStr);
        outMsg.setUsersStr(usersStr);
        outMsg.setDesignStr(designStr);

        //3.材料图片列表
        List<String> matlist = findCtcPicById(ctcid, 1);
        outMsg.setMatlist(matlist);

        //4.获取流程信息详情
        if (StringUtils.isEmpty(outMsg.getFlowId())){
            return outMsg;
        }

        Map<String,String> map = new HashMap<>();
        map.put("ctcId", ctcid);
        DecFlowFindCondition inf = decCtctionMapper.findFlowInf(map);
        outMsg.setFlowlist(inf);

        return outMsg;
    }

    /**
     * 小程序查询项目列表(项目参数等)
     * @param user
     * @param inpo
     * @return
     */
    @Override
    public JsonResult searchCtcList(DecorationUser user, DecCtctionFindCondition inpo) {
        Integer sum = 0;
        JsonResult jsonResult = new JsonResult();
        //如果为项目类型值为 -1 全部展示
        if (inpo.getCtcProgress() == -1){
            inpo.setCtcProgress(null);
        }

        Map<String, Object> param = new HashMap<String, Object>();
        List<DecCtctionInfVo> results = null;

        //设置分页信息，mabatis 拦截器拼接sql分页参数
        PageHelper.startPage(inpo.getPage(), inpo.getDataCount());

        if (user.getPerType() == 0){  //管理，可查全部类型项目列表
            param.put("ctcProgress", inpo.getCtcProgress());
            results = decCtctionMapper.ctcList(param);

        }else if(user.getPerType() == 1) {//师傅，只查该用户名下项目列表
            param.put("userId", user.getUserId());
            param.put("ctcProgress", inpo.getCtcProgress());
            results= decCtctionMapper.findCtcList(param);
        }else{  //监理，业主，设计师
            param.put("userId", user.getUserId());
            param.put("ctcProgress", inpo.getCtcProgress());

            results= decCtctionMapper.searchCtcList(param);
        }
        PageInfo<DecCtctionInfVo> pageInfo = new PageInfo(results);
        // System.out.println("总页数: "+pageInfo.getPages());  //总页数
//        System.out.println("当前页数: "+ pageInfo.getPageNum());//当前页码
//        System.out.println("当前页数量: " + pageInfo.getSize()); //当前页数据量
        // System.out.println("总数量: "+pageInfo.getTotal());  //
        //输入每页大小 pageInfo.getPageSize();
        sum = Integer.parseInt(String.valueOf(pageInfo.getTotal()));
        PageHelper.clearPage();

        List<DecCtctionInMsg> respo = new ArrayList<DecCtctionInMsg>();

        if ( results.size() <1){
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            jsonResult.setMsg("获取项目列表为空");
            jsonResult.setCount(0);
            jsonResult.setData(respo);
            return jsonResult;
        }

        for (DecCtctionInfVo vo: results){
            DecCtctionInMsg po = new DecCtctionInMsg();

            po.setCtcid(vo.getCtcId());
            po.setCtcName(vo.getCtcName());
            po.setCtcDesc(vo.getCtcDesc());
            po.setCtcAddress(vo.getCtcAddress());
            if (vo.getCtcType() != null) {
                po.setCtcType(vo.getCtcType() == 0 ? null : vo.getCtcType());
            }
            po.setStartTime(vo.getStartTime());
            po.setCtcProgress(vo.getCtcProgress());
            if (StringUtil.isEmpty(vo.getCtcPlan())){
                po.setCtcPlan(new ArrayList<>());
            }else {
                po.setCtcPlan(StringUtil.StrAsArray(vo.getCtcPlan()));
            }
            //查找项目阶段名称
            DecPhaseInf pahsePo = null;
            try {
                pahsePo = decCtctionMapper.searchPhaseNameByCtcId(vo.getCtcId());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (pahsePo != null) {
                po.setPhaseName(pahsePo.getPhaseName());
                po.setFlowId(pahsePo.getFlowId());
            }
            respo.add(po);
        }

        jsonResult.setCode(JsonResult.SUCCESS_CODE);
        jsonResult.setMsg("获取项目列表成功");
        jsonResult.setCount(sum);
        jsonResult.setData(respo);
        return jsonResult;
    }

    /**
     * 后端模糊查询项目列表(项目参数等)
     * @param findCondition
     * @return
     */
    @Override
    public JsonResult findCtcList(DecCtctionFindCondition findCondition) {

        log.info("ctcProgress["+findCondition.getCtcProgress()+"],ctcType["+findCondition.getCtcType()
                +"],str["+findCondition.getStr()+"]");
        Integer sum = 0;
        JsonResult jsonResult = new JsonResult();
        //参数校验
        if (findCondition.getCtcProgress() == -1){
            findCondition.setCtcProgress(null);
        }
        if (findCondition.getCtcType() == -1){
            findCondition.setCtcType(null);
        }
        if(findCondition.getStr() !=null && findCondition.getStr().trim().equals("")){
            findCondition.setStr(null);
        }


        //如果不是公司管理，只查该用户名下项目列表，如果是其他，可查全部类型列表
        log.info("校验用户角色权限");
        DecorationUser result = decUserMapper.findById(findCondition.getUserId());
        if (result == null){
            return null;
        }
        Map<String, Object> param = new HashMap<String, Object>();
        List<DecCtctionInfVo> results = null;

        if (result.getPerType() != 0){
            //查项目关系表，获取项目id列表，再查项目列表
            return null;
        }else {
            param.put("ctcProgress", findCondition.getCtcProgress());
            param.put("ctcType", findCondition.getCtcType());
            param.put("str", findCondition.getStr());

            log.info("页数:"+findCondition.getPage() +" 展示数量:"+ findCondition.getDataCount());
            PageHelper.startPage(findCondition.getPage(), findCondition.getDataCount());
            results = decCtctionMapper.searchCtcLists(param);
            PageInfo<DecCtctionInfVo> pageInfo = new PageInfo(results);
            sum = Integer.parseInt(String.valueOf(pageInfo.getTotal()));
            PageHelper.clearPage();
        }
        log.info(" 组装最后结果 ");
        List<DecCtctionInMsg> respo = new ArrayList<DecCtctionInMsg>();
        //查询结果为空
        if ( results.size() <= 0){
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            jsonResult.setMsg("获取项目列表信息0条");
            jsonResult.setCount(sum);
            jsonResult.setData(respo);
            return jsonResult;
        }

        // 项目列表信息展示
        for (DecCtctionInfVo vo: results){
            DecCtctionInMsg po = new DecCtctionInMsg();

            po.setCtcid(vo.getCtcId());
            po.setCtcName(vo.getCtcName());
            po.setCtcAddress(vo.getCtcAddress());
            po.setCtcDesc(vo.getCtcDesc());
            po.setCtcProgress(vo.getCtcProgress());
            if (vo.getCtcType() != null) {
                po.setCtcType(vo.getCtcType() == 0 ? null : vo.getCtcType());
            }
            po.setStartTime(vo.getStartTime());
            po.setFlowId(vo.getFlowId());

            //查询项目相关人员信息
             log.info("查询项目相关人员信息");
            Map<String,Object> map = new HashMap<>();
            map.put("ctcid", vo.getCtcId());
            map.put("perType",3);
            List<DecUserBase> userlist = null;
            try {
                userlist = decCtcRelMapper.searchCtcRelByIdAndType(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (userlist != null && userlist.size() > 0) {
                po.setUserArray(getUserlist(userlist));
            }
            //查找项目阶段名称
            log.info("查找项目阶段信息");
            DecPhaseInf pahsePo = null;
            try {
                pahsePo = decCtctionMapper.searchPhaseNameByCtcId(vo.getCtcId());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (pahsePo != null){
                po.setPhaseName(pahsePo.getPhaseName());
                po.setFlowId(pahsePo.getFlowId());
            }else{
                po.setPhaseName(null);
//                po.setFlowId(null);
                /*if (StringUtil.isNotEmpty(po.getFlowId())){
                }*/
            }

            respo.add(po);
        }

        //3.返回数据
        if ( respo.size()< 1 ){
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            jsonResult.setMsg("获取项目列表信息0条");
            jsonResult.setCount(sum);
            return jsonResult;
        }
        jsonResult.setCode(JsonResult.SUCCESS_CODE);
        jsonResult.setMsg("获取项目列表信息成功");
        jsonResult.setCount(sum);
        jsonResult.setData(respo);
        return jsonResult;
    }

    /**
     *   通过项目id和图片类型id，查找项目关联材料内容图片地址,目前只查材料图片
     * @param ctcid
     * @param picType
     * @return
     */
    public List<String> findCtcPicById(String ctcid, Integer picType) {
        if (picType != 1){

            return null;
        }
        List<String> piclist = decPictureMapper.searchCtcRel(ctcid);
        return piclist;
    }

    /**
     *   根据用户id 及 用户类型，获取用户基本信息
     * @param userids
     * @param pertype
     * @return
     */
    public List<DecUserBase> makeUser(List<String> userids, Integer pertype) {
        if (userids == null || userids.size() < 1) {
            return null;
        }
        List<DecUserBase> resultlist = new ArrayList<>();

        for (String userid : userids) {

            DecUserBase result = decUserMapper.findCtcRelById(userid);
            if (result == null){
                log.info("用户"+userid+" 的信息不存在");
                continue;
            }
            result.setPerType(pertype);

            resultlist.add(result);
        }
        return resultlist;
    }

    /**
     *  新增项目关联用户
     * @param userlist
     * @param ctcid
     * @param userid
     */
    public void addCtcRelUsers(List<DecUserBase> userlist, String ctcid, String userid) {
        // 新增监理关联信息
        for (DecUserBase supuser : userlist) {
            DecorationCtctionRelation ctcrelPo = new DecorationCtctionRelation();
            ctcrelPo.setCtcId(ctcid);
            ctcrelPo.setUserId(supuser.getUserId());
            ctcrelPo.setPicId(null);
            ctcrelPo.setFlag(0);
            ctcrelPo.setRelatedName(supuser.getPerName());
            ctcrelPo.setRelatedType(supuser.getPerType());
            ctcrelPo.setCreateTime(new Date());
            ctcrelPo.setUpdateTime(new Date());
            ctcrelPo.setCreateId(userid);
            ctcrelPo.setUpdateId(userid);

            int result = decCtcRelMapper.addCtcReal(ctcrelPo);
            if (result != 1) {
                log.info(supuser.getPerName() + "信息，插入项目关联表异常");
            }
        }
    }

    /**
     *  删除项目关联表数据
     * @param userlist
     * @param ctcid
     */
    public void deleteCtcRelUser(List<DecUserBase> userlist, String ctcid) {
        for (DecUserBase base : userlist) {
            int result = decCtcRelMapper.deleteCtc(base.getUserId(), ctcid);
            if (result != 1) {
                log.error(base.getPerName() + "信息，删除项目关联表异常");
            }
        }

    }

    /**
     * 新增项目材料图片
     * @param picture
     * @param ctcId
     * @param userid
     */
    public void addPicture(List<String> picture, String ctcId, String userid) {
        //1.先删除
        int num = decPictureMapper.deleteCtcRelPic(ctcId);
        if (num <= 0) {
            log.info(picture.toString() + "信息，删除图片表异常，项目id为: " + ctcId);
        }
        //2.后新增
        for (String address : picture) {
            String picInfId = String.valueOf(SnowFlake.getInstance().nextId());
            DecorationPicture pic = new DecorationPicture();
            pic.setPicInfId(picInfId);
            pic.setUserId(userid);
            pic.setPicAddress(address);
            pic.setCtcId(ctcId);
            pic.setFirFlag(1);

            int result = 0;
            try {
                result = decPictureMapper.add(pic);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (result != 1) {
                log.info(address + "信息，插入图片表异常,项目id为: " + ctcId);
            }
        }
    }
    
    /**
     * 合并list集合内容，项目关联用户专用
     **/
    public List<DecUserBase> mergeList(List<DecUserBase>... lists) {
        List<DecUserBase> result = new ArrayList<DecUserBase>();
        for (List<DecUserBase> list : lists) {
            if (list == null) {
                continue;
            }
            for (DecUserBase base : list) {
                if (base == null) {
                    continue;
                }
                result.add(base);
            }
        }
        return result;
    }

    /**
     * 按类型分割项目关联用户信息，返回用户名称
     **/
    public List<String> separateList(List<DecUserBase> list,Integer userType){
        List<String> out = new ArrayList<String>();
        for (DecUserBase base:list){
            if (base.getPerType() == userType){
                out.add(base.getPerName());
            }
        }
        return out;
    }

    @Override
    public List<DecCtctionPo> getCtctionList(DecCtctionFindCondition findCondition) {
        return decCtctionMapper.getCtctionList(findCondition);
    }

    @Override
    public int getCtctionListSize(DecCtctionFindCondition findCondition) {
        return decCtctionMapper.getCtctionListSize(findCondition);
    }

    /**
     *   获取用户下拉框列表
     * @param perType
     * @return
     */
    @Override
    public List<DecPhaseBase> findUserTypelist(Integer perType) {
        return decCtctionMapper.findUserTypelist(perType);
    }

    //将用户对象数组转换为用户名称字符串
    private String getUserlist(List<DecUserBase> userlist){
        StringBuffer result = new StringBuffer();
        if (userlist.size() > 0){
            int i = 1;
            for (DecUserBase user : userlist){
                result.append(user.getPerName());
                if (userlist.size() != i){
                    result.append(",");
                }
                i++;
            }
        }

        return result.toString();
    }

    //关联流程
    private void buildRealFlow(String userid,String oldFlowId,String ctcId){
        String newflowId = String.valueOf(SnowFlake.getInstance().nextId());

        Map<String,String> map = new HashMap<String,String>();
        map.put("newflowId",newflowId );
        map.put("per_id", userid);
        map.put("ctcId", ctcId);
        map.put("oldflowId", oldFlowId);
        decCtctionMapper.relaFlowId(map);

        List<DecPhaseInf> relphaselist = decCtctionPhaseMapper.ctcPhaselist(oldFlowId);
        for (DecPhaseInf phaseInf: relphaselist){
            String phaseId = String.valueOf(SnowFlake.getInstance().nextId());
            phaseInf.setCtcId(ctcId);
            phaseInf.setFlowId(newflowId);
            phaseInf.setPhaseId(phaseId);
            if (phaseInf.getPhaseRank() == 1){
                phaseInf.setVaildFlag(1);
            }
            decCtctionPhaseMapper.addPhase(phaseInf);
        }
    }

    private void buildDecCtctionOutMsg(DecCtctionOutMsg outMsg,DecCtctionPo inpo){
        outMsg.setCtcid(inpo.getCtcid());
        outMsg.setCtcName(inpo.getCtcName());
        outMsg.setCtcAddress(inpo.getCtcAddress());
        outMsg.setCtcDesc(inpo.getCtcDesc());
        outMsg.setCtcProgress(inpo.getCtcProgress());
        outMsg.setCtcType(inpo.getCtcType());
        outMsg.setStartTime(inpo.getStartTime());
        outMsg.setEndTime(inpo.getEndTime());
        outMsg.setForecastTime(inpo.getForecastTime());
        outMsg.setFlowId(inpo.getFlowId());
        outMsg.setPhaseName(inpo.getPhaseName());
        outMsg.setRemark(inpo.getRemark());
    }
}
